<template>
  <a-modal
    :open="open"
    width="640px"
    title="调整下达时间"
    centered
    destroy-on-close
    :ok-button-props="{ disabled: !isCompleted }"
    @ok="onOk"
    @cancel="onCancel"
  >
    <div class="mb-5">
      <div class="flex my-5">
        <div>{{ store.currentPatient.wardName || '-' }}</div>
        <div class="mx-3">{{ store.currentPatient.lastBedCode || '-' }}床</div>
        <div>{{ store.currentPatient.brName || '-' }}</div>
        <div class="mx-3">{{ store.currentPatient.brXb || '-' }}</div>
        <div>{{ store.currentPatient.brAge || '-' }}</div>
      </div>
      <div>
        <PreviewCell :row-data="rowData" />
      </div>
    </div>
    <a-divider />
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label="原下达时间" class="w-1/2">
        <span>{{ formState.oldTime || '-' }}</span>
      </a-form-item>
      <a-form-item label="调整时间" class="w-1/2" v-bind="validateInfos.issueTime">
        <a-date-picker v-model:value="formState.issueTime" show-time placeholder="请选择" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="2" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import PreviewCell from '@/views/in-doctor/workstation/components/TabDoctorAdvice/PreviewCell'
import useStore from '../../store'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const store = useStore()
const formState = ref({
  issueTime: ''
})
const rulesState = ref({
  issueTime: [{ required: true, message: '请选择日期' }]
})

const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  const { code, msg } = await post('/inp/zyys/doModifyIssueTime', { id: props.rowData?.id, ...formState.value })
  if (code !== 0) return message.error(msg || '保存失败')
  message.success(msg)
  emit('success')
  onCancel()
}

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}

watch(
  () => props.open,
  (val) => {
    !val && onCancel()
    formState.value.oldTime = props.rowData?.issueTime
  }
)
</script>
<style lang="less" scoped></style>
